package com.czwj.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.InspectionItem;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.InspectionItemService;
import com.czwj.util.TextUtil;

@Controller
public class InspectionItemAction {
	@Resource
	InspectionItemService inspectionItemService;

	@RequestMapping(value = "/inspectionitem", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<InspectionItem> inspectionItem(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status) {
		DataGridData<InspectionItem> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(status!=null){
			map.put("status", status);
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getId()==1){
			
		}else{
			map.put("companyid",userInfo.getCompanyid());
		}
		List<InspectionItem> list = inspectionItemService.search(map);
		result.setRows(list);
		result.setTotal(inspectionItemService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/inspectionItemlist", method = RequestMethod.POST)
	@ResponseBody
	public List<InspectionItem> inspectionItemlist() {
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyid",userInfo.getCompanyid());
		List<InspectionItem> list = inspectionItemService.search(map);
		return list;
	}

	@SystemControllerLog(description = "修改安全巡检模板")
	@RequestMapping(value = "/inspectionItemSave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult inspectionItemSave(@RequestBody InspectionItem inspectionItem) throws Exception {
		OperateResult result = new OperateResult();
		int count = 0;
		if (inspectionItem.getId() != null && inspectionItem.getId() > 0) {
			count = inspectionItemService.update(inspectionItem);
		} else {
			Map<String, Object> map=new HashMap<String, Object>();
			List<InspectionItem> list=inspectionItemService.search(map);
			int MaxSort=list.size();
			inspectionItem.setSort(MaxSort+1);
			inspectionItem.setStatus(2);
			Subject currentUser = SecurityUtils.getSubject();
			UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
			inspectionItem.setCompanyid(userInfo.getCompanyid());
			count = inspectionItemService.insert(inspectionItem);
		}
		result.setSuccess(count > 0);
		return result;
	}

	@SystemControllerLog(description = "禁用安全巡检模板")
	@RequestMapping(value = "inspectionItemDelete.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult deleteInspectionItem(@RequestParam("inspectionItemid") int id) {
		OperateResult result = new OperateResult();
		try {
			int count = inspectionItemService.updateStatus(id);
			result.setSuccess(count > 0);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@RequestMapping(value = "inspectionItemOpen.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult openInspectionItem(@RequestParam("inspectionItemid") int id) {
		OperateResult result = new OperateResult();
		try {
			int count = inspectionItemService.updateStatusOpen(id);
			result.setSuccess(count > 0);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@RequestMapping(value = "/inspectionItemSort", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult inspectionItemSort(@RequestParam(value = "inspectionItemId", required = false) int inspectionItemId,
			@RequestParam(value = "operation", required = false) int operation) {
		OperateResult result = new OperateResult();
		Map<String, Object> map = new HashMap<String, Object>();
		InspectionItem ii = inspectionItemService.selectByPrimaryKey(inspectionItemId);
		int count;
		int count2;
			if (operation == 1) {
				// 上移
				map.put("sort", ii.getSort() - 1);
				InspectionItem ii0 = inspectionItemService.searchBySort(map);
				if (ii0 != null) {
					count = inspectionItemService.sortReduce(ii);
					count2 = inspectionItemService.sortAdd(ii0);
				} else {
					count = count2 = 0;
				}
			} else {
				// 下移
				map.put("sort", ii.getSort() + 1);
				InspectionItem ii1 = inspectionItemService.searchBySort(map);
				if (ii1 != null ) {
					count = inspectionItemService.sortAdd(ii);
					count2 = inspectionItemService.sortReduce(ii1);
				} else {
					count = count2 = 0;
				}
			}
		
		result.setSuccess(count > 0 && count2 > 0);
		return result;
	}
}
